﻿using OrderSystem.Common;
using OrderSystem.Templet;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormQueryInvoices : FormTemplet
    {
        private int _nCustomerID = -1;

        private int _nProjectID = -1;

        public FormQueryInvoices()
        {
            this.InitializeComponent();
        }

        private void FormQueryOrders_Load(object sender, EventArgs e)
        {
            this.dtpDateStart.Enabled = false;
            this.dtpDateEnd.Enabled = false;
            this.chkLockedData.Enabled = false;
            this.chkCancelled.Enabled = false;
        }

        private void btnSelectCustomer_Click(object sender, EventArgs e)
        {
            CustomerData customerData = FormSelectCustomer.ShowBox();
            if (customerData != null)
            {
                this._nCustomerID = customerData.CustomerID;
                this.txtCustomerName.Text = customerData.CompanyName + "(" + customerData.Linkman + ")";
                this._nProjectID = -1;
                this.txtProjectName.Text = "";
            }
        }

        private void btnSelectProject_Click(object sender, EventArgs e)
        {
            ProjectData projectData = FormSelectProject.ShowBox(this._nCustomerID);
            if (projectData != null)
            {
                this._nProjectID = projectData.ProjectID;
                this.txtProjectName.Text = projectData.ProjectName + "(" + projectData.Linkman + ")";
            }
        }

        private void chkDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkDateUsed.Checked)
            {
                this.dtpDateStart.Enabled = true;
                this.dtpDateEnd.Enabled = true;
            }
            else
            {
                this.dtpDateStart.Enabled = false;
                this.dtpDateEnd.Enabled = false;
            }
        }

        private void chkLockedDataUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkLockedDataUsed.Checked)
            {
                this.chkLockedData.Enabled = true;
            }
            else
            {
                this.chkLockedData.Enabled = false;
                this.chkLockedData.Checked = false;
            }
        }

        private void chkCancelledUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkCancelledUsed.Checked)
            {
                this.chkCancelled.Enabled = true;
            }
            else
            {
                this.chkCancelled.Enabled = false;
                this.chkCancelled.Checked = false;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            InvoiceCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                FormInvoiceList formInvoiceList = new FormInvoiceList(conditionForQuery);
                if (formInvoiceList != null && !formInvoiceList.IsDisposed)
                {
                    formInvoiceList.MdiParent = base.MdiParent;
                    formInvoiceList.Show();
                    formInvoiceList.Activate();
                }
            }
        }

        private void btnShowSumUp_Click(object sender, EventArgs e)
        {
            InvoiceCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                FormInvoiceSumUp formInvoiceSumUp = new FormInvoiceSumUp(conditionForQuery);
                if (formInvoiceSumUp != null && !formInvoiceSumUp.IsDisposed)
                {
                    formInvoiceSumUp.MdiParent = base.MdiParent;
                    formInvoiceSumUp.Show();
                    formInvoiceSumUp.Activate();
                }
            }
        }

        private InvoiceCondition GetConditionForQuery()
        {
            InvoiceCondition invoiceCondition = new InvoiceCondition();
            InvoiceCondition result;
            if (this.txtCustomerName.Text.Trim() == "" && this.txtProjectName.Text.Trim() == "" && !this.chkDateUsed.Checked && !this.chkLockedDataUsed.Checked && !this.chkCancelledUsed.Checked && this.txtInvoiceNumber.Text.Trim() == "" && this.txtRemark.Text.Trim() == "")
            {
                MessageBox.Show("至少要输入一条查询信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                result = null;
            }
            else
            {
                if (this.txtCustomerName.Text.Trim() != "")
                {
                    if (this.txtCustomerName.Text.Trim() == "" || this._nCustomerID == -1)
                    {
                        MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtCustomerName.Focus();
                        result = null;
                        return result;
                    }
                    invoiceCondition.CustomerID = this._nCustomerID;
                }
                if (this.txtProjectName.Text.Trim() != "")
                {
                    if (this.txtProjectName.Text.Trim() == "" || this._nProjectID == -1)
                    {
                        MessageBox.Show("请选择计划", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtProjectName.Focus();
                        result = null;
                        return result;
                    }
                    invoiceCondition.ProjectID = this._nProjectID;
                }
                if (this.chkDateUsed.Checked)
                {
                    int num = DateTime.Parse(this.dtpDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
                    if (num > 0)
                    {
                        MessageBox.Show("发货日期：后面的日期要 大于 或 等于前面的日期！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dtpDateEnd.Focus();
                        result = null;
                        return result;
                    }
                    invoiceCondition.InvoiceDateStart = this.dtpDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00";
                    invoiceCondition.InvoiceDateEnd = this.dtpDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59";
                }
                if (this.chkLockedDataUsed.Checked)
                {
                    if (this.chkLockedData.Checked)
                    {
                        invoiceCondition.Locked = 1;
                    }
                    else
                    {
                        invoiceCondition.Locked = 0;
                    }
                }
                if (this.chkCancelledUsed.Checked)
                {
                    if (this.chkCancelled.Checked)
                    {
                        invoiceCondition.Cancelled = 1;
                    }
                    else
                    {
                        invoiceCondition.Cancelled = 0;
                    }
                }
                if (this.txtInvoiceNumber.Text.Trim() != "")
                {
                    invoiceCondition.InvoiceCode = this.txtInvoiceNumber.Text.Trim();
                }
                if (this.txtRemark.Text.Trim() != "")
                {
                    if (this.txtRemark.Text.Trim().Length > 64)
                    {
                        MessageBox.Show("字符数量不能大于64！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtRemark.Focus();
                        result = null;
                        return result;
                    }
                    invoiceCondition.Remark = this.txtRemark.Text.Trim();
                }
                result = invoiceCondition;
            }
            return result;
        }

        private void dtpDateStart_ValueChanged(object sender, EventArgs e)
        {
            int num = DateTime.Parse(this.dtpDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
            if (num > 0)
            {
                this.dtpDateEnd.Value = this.dtpDateStart.Value;
            }
        }

        private void dtpDateEnd_ValueChanged(object sender, EventArgs e)
        {
            int num = DateTime.Parse(this.dtpDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
            if (num > 0)
            {
                this.dtpDateStart.Value = this.dtpDateEnd.Value;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
